package com.blank.white.common.util;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;

/**
 * @desc 描述一下该类的作用
 * @classname LoggerUtil
 */
public class LoggerUtil {
    /**
     * 按日志格式化代码
     * eg: format("level:{},date{}",new Object[]{"info","2020-06-03", new RuntimeException("error msg")}
     *
     * @param messagePattern
     * @param argArray
     * @return
     */
    public static final String format(final String messagePattern, final Object... argArray) {
        FormattingTuple formattingTuple = MessageFormatter.arrayFormat(messagePattern, argArray);
        if (formattingTuple.getThrowable() != null) {
            String fullStackTrace = ExceptionUtils.getStackTrace(formattingTuple.getThrowable());
            /*
            commin-lang的全部打印
            String fullStackTrace = ExceptionUtils.getFullStackTrace(formattingTuple.getThrowable());
            */
            return formattingTuple.getMessage() + System.lineSeparator() + fullStackTrace;
        }
        return formattingTuple.getMessage();
    }
}
